var initPort = (item) => {
  let TypeID = item.TypeID
  let color = TypeID == 9 ? '#7B187D' : (TypeID == 10 ? '#48C911' : (TypeID == 12 ? '#FFD900' : (TypeID == 4 ?
    '#D4FF00' :
    '#EAFF00')))
  let position = TypeID != 4 && TypeID != 9 && TypeID != 10 && TypeID != 11 && TypeID != 12 ? 'top' : 'bottom'
  return {
    id: item._ID,
    label: {
      position: 'top', // 标签位置
    },
    group: 'line',
    attrs: {
      text: {
        text: item.handlerDefines.length,
      },
      circle: {
        r: 10,
        magnet: position == "bottom" ? true : 'passive',
        stroke: position == "bottom" ? color : '#31d0c6',
        strokeWidth: 2,
        fill: position == "bottom" ? color : '#e0e0e0',
      },
    },
  }
}
var markup = (item) => {
  return [{
      tagName: 'rect',
      selector: 'body',

    },
    {
      tagName: 'text',
      selector: 'label',
    },
    {
      tagName: 'text',
      selector: 'time',
    },
    {
      tagName: 'g',
      attrs: {
        class: 'btn add',
      },
      children: [{
          tagName: 'circle',
          attrs: {
            class: 'add',
          },
        },
        {
          tagName: 'text',
          attrs: {
            class: 'add',
          },
        },
      ],
    },
    {
      tagName: 'g',
      attrs: {
        class: 'btn del',
      },
      children: [{
          tagName: 'circle',
          attrs: {
            class: 'del',
          },
        },
        {
          tagName: 'text',
          attrs: {
            class: 'del',
          },
        },
      ],
    },
  ]
}
var shapeAttrs = (item) => {
  return {
    init: {
      initY: (item.layer) * 105 + 30,
      ...item
    },
    body: {
      fill: item.color,
      stroke: '#000',
      strokeWidth: 2,
    },
    label: {
      text: item.name,
      refY: 0.25,
      fontSize: 16,
    },
    time: {
      text: item._ActStart + "-" + item._ActEnd,
      refX: 0.5,
      refY: 0.5,
      fontSize: 14,
    },
    '.btn.add': {
      refX: 10,
      refY: -15,
      event: 'btn:add',
    },
    '.btn.del': {
      refX: 50,
      refY: -15,
      event: 'btn:del',
    },
    '.btn > circle': {
      r: 10,
      fill: 'transparent',
      stroke: '#333',
      strokeWidth: 1,
    },
    '.btn.add > text': {
      fontSize: 20,
      fontWeight: 800,
      stroke: '#0051FF',
      refX: 0,
      refY: 0,
      text: '+',
    },
    '.btn.del > text': {
      fontSize: 28,
      fontWeight: 500,
      stroke: '#AB0303',
      refX: 0,
      refY: 0,
      text: '-',
    },
  }
}
var shapeRect = (item) => {
  let ports = []
  for (let i = 0; i < item.cfgEventCondition.length; i++) {
    let key = item.cfgEventCondition[i]
    ports.push(initPort(key))
  }
  let width = (item._ActEnd - item._ActStart) * 10 * 4
  return {
    id: item._ID == 0 ? "0" : item._ID,
    shape: "rect",
    x: item.initX,
    y: (item.layer) * 105 + 30,
    width: width,
    height: 60,
    markup: markup(item),
    attrs: shapeAttrs(item),
    ports: {
      groups: {
        line: {
          position: 'bottom',
        }
      },
      items: [...ports],
    },
  }
}

export default {
  listData: (list) => {
    let nodes = []
    for (let i = 0; i < list.length; i++) {
      let item = list[i]
      for (let k = 0; k < item.items.length; k++) {
        let key = item.items[k]
        let _ActStart = 0
        let _ActEnd = 0
        _ActStart = parseInt(key.initX / 4) / 10
        _ActEnd = _ActStart + 5
        key._ActStart = _ActStart
        key._ActEnd = _ActEnd
        key.layer = i
        let rect = shapeRect(key)
        let obj = {
          ...key,
          ...rect,
        }
        item.items[k] = {
          ...obj
        }
        nodes.push({
          ...obj
        })
      }
    }
    return nodes
  }
}
